package com.joe.updemo.utils.netWorkUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import com.joe.updemo.utils.manager.ExceptionUtil;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

import java.util.concurrent.TimeUnit;


/**
 * @类描述： 是一个检测网络状态改变的，需要配置
 * <receiver
 * android:name="com.lidroid.zutils.http.netstate.NetworkStateReceiver" >
 * <intent-filter> <action
 * android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action
 * android:name="android.gzcpc.conn.CONNECTIVITY_CHANGE" />
 * </intent-filter> </receiver>
 * 需要开启权限 <uses-permission
 * android:name="android.permission.CHANGE_NETWORK_STATE" />
 * <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
 * /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
 * />
 */
public class NetworkStateReceiver extends BroadcastReceiver {
    private final static String ZW_ANDROID_NET_CHANGE_ACTION = "zw.android.net.conn.CONNECTIVITY_CHANGE";
    private final static String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    private static NetworkStateReceiver receiver;
    private NetworkUtil.NetType netType;
    private NetworkUtil.NetType type;
    private NetWorkChangeCallBack mNetworkstatuschangelistener;
    private Context mContext;

    private NetworkStateReceiver() {

    }

    /**
     * @return BroadcastReceiver 返回类型
     * @information:获得广播实例
     */
    public static NetworkStateReceiver getInstance() {
        if (receiver == null) {
            synchronized (NetworkStateReceiver.class) {
                if (receiver == null) {
                    receiver = new NetworkStateReceiver();
                }
            }
        }
        return receiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        NetworkUtil.init(context);
        type = NetworkUtil.getNetworkType();
        preNotify();
    }

    private void preNotify() {
        Observable.timer(5, TimeUnit.SECONDS).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                notifyObserver();
            }
        });
    }

    private void notifyObserver() {
        if (type == netType) {
            return;
        }
        if (type == NetworkUtil.NetType.NETWORK_UNKNOWN && netType != NetworkUtil.NetType.NETWORK_UNKNOWN) {
            Log.d("junfutong", " netToUnknow");
            if (null != mNetworkstatuschangelistener) {
                mNetworkstatuschangelistener.netToUnknow(type);
            }
        } else if (type != NetworkUtil.NetType.NETWORK_UNKNOWN && netType == NetworkUtil.NetType.NETWORK_UNKNOWN) {
            Log.d("junfutong", " unknowToNet");
            if (null != mNetworkstatuschangelistener) {
                mNetworkstatuschangelistener.unknowToNet(type);
            }
        } else {
            Log.d("junfutong", " connectiontypelistenerNet");
            if (null != mNetworkstatuschangelistener) {
                mNetworkstatuschangelistener.connectionType(type);
            }
        }
        netType = type;
    }

    /**
     * 注册网络连接观察者
     */
    public void registerObserver(Context context, NetWorkChangeCallBack networkstatuschangelistener) {
        mContext = context;
        mNetworkstatuschangelistener = networkstatuschangelistener;
        registerNetworkStateReceiver();
        netType = NetworkUtil.getNetworkType();
    }

    /**
     * 注销网络连接观察者
     */
    public void removeRegisterObserver() {
        unRegisterNetworkStateReceiver();
    }

    /**
     * 注册网络状态广播
     */
    private void registerNetworkStateReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ZW_ANDROID_NET_CHANGE_ACTION);
        filter.addAction(ANDROID_NET_CHANGE_ACTION);
        mContext.registerReceiver(receiver, filter);
    }


    /**
     * 注销网络状态广播
     */
    private void unRegisterNetworkStateReceiver() {
        if (receiver != null) {
            try {
                mContext.getApplicationContext().unregisterReceiver(receiver);
            } catch (Exception e) {
                ExceptionUtil.getInstance().saveExceptionFile("NetworkStateReceiver " +
                        "unregisterReceiver error:" + e.getMessage());
                e.printStackTrace();
            }
        }

    }
}